﻿using System;
using System.Collections.Generic;

using E.Enum;
using E.BLL;
using E.Model;
using E.Web.UI.Page;

using X.Common;

namespace E.Web.Teacher.teacher
{
    public partial class Edit : TeacherBasePage
    {
        public IList<DictAreaModel> areaList;
        public TeacherModel model = new TeacherModel();
        public IDictionary<int, string> authorityList = new Dictionary<int, string>();
        public Dictionary<int, string> projectList = new Dictionary<int, string>();
        public string act;
        public int id;
        public string area_v;
        public bool isInternal = false;
        //public IDictionary<int, string> questionList;

        protected override void ShowPage()
        {
            id = XRequest.GetInt("id", -1);
            act = XRequest.GetString("act");

            authorityList = EnumClass.GetAuthorityList(a2);
            projectList = (new CourseBLL()).GetInternalProjectList();
            //questionList = (new UserBLL()).GetForgetPwdQuestionList();

            TeacherBLL bll = new TeacherBLL();

            if (ispost)
            {
                string name = XRequest.GetString("NameTxt");
                bool gender = XRequest.GetInt("GenderRg", -1) == 1 ? true : false;
                string email = XRequest.GetString("EmailTxt");
                string area = XRequest.GetString("area_v");
                string[] areas = GetArray(area);
                //int questionid = XRequest.GetInt("QuestionSlt", -1);
                //string answer = XRequest.GetString("AnswerTxt");

                if (CheckValue())
                {
                    model = new TeacherModel();
                    model.Name = name;
                    model.Email = email;
                    model.Gender = gender;
                    model.AreaStrList = areas;
                    //model.ForgetPwdQuestionID = questionid;
                    //model.ForgetPwdAnswer = answer;
                    model.ForgetPwdQuestionID = 1;
                    model.ForgetPwdAnswer = "111111";

                    int rst = 0;
                    string href = "";

                    switch (act)
                    {
                        case "n":
                            string loginid = XRequest.GetString("LoginIDTxt");
                            string pwd = XRequest.GetString("PwdTxt");
                            int certificatedid = XRequest.GetInt("ProjectSlt", -1);

                            model.LoginID = loginid;
                            model.Pwd = Utils.MD5(pwd);
                            model.Authority = (EnumTeacherAuthority)XRequest.GetInt("AuthoritySlt", -1);

                            if (model.Authority == EnumTeacherAuthority.Internals)
                            {
                                model.CertificateID = certificatedid;
                            }
                            else
                            {
                                model.CertificateID = -1;
                            }

                            rst = bll.AddTeacher(model);

                            href = "/teacher/Edit.aspx?act=n";

                            break;
                        case "u":
                            model.ID = id;
                            model.Authority = (EnumTeacherAuthority)XRequest.GetInt("aid", -1);
                            model.CertificateID = XRequest.GetInt("cerid", -1);

                            rst = bll.UpdateTeacherWithArea(model);

                            href = "/teacher/Edit.aspx?act=u&id=" + id;
                            break;
                        default:
                            break;
                    }

                    if (rst > 0)
                    {
                        ExecuteJS("alert('操作成功'); window.location = '" + href + "';");
                    }
                    else
                    {
                        Alert("操作失败");
                    }
                }
            }
            else
            {
                areaList = GetTeacherAreaList(teacherid);
                model = bll.GetTeacherInfo(id);

                IList<int> areaIDList = bll.GetTeacherAreaIDList(model.ID);
                area_v = Convert2String(areaIDList);
            }
        }

        private bool CheckValue()
        {
            bool rst = true;

            if (XRequest.GetString("act") == "n" && XRequest.GetString("LoginIDTxt").Equals(""))
            {
                Alert("登录名不能为空");
                rst = false;
            }
            if (XRequest.GetString("act") == "n" && XRequest.GetString("PwdTxt").Equals(""))
            {
                Alert("密码不能为空");
                rst = false;
            }
            if (XRequest.GetString("NameTxt").Equals(""))
            {
                Alert("姓名不能为空");
                rst = false;
            }
            if (XRequest.GetString("act") == "n" && XRequest.GetInt("AuthoritySlt", -1) == -1)
            {
                Alert("请选择权限");
                rst = false;
            }
            if (XRequest.GetString("act") == "n" && XRequest.GetInt("AuthoritySlt", -1) == 2 && XRequest.GetInt("AuthoritySlt", -1) == -1)
            {
                Alert("请选择项目");
                rst = false;
            }
            if (XRequest.GetString("area_v").Equals(""))
            {
                Alert("请选择至少一个地区");
                rst = false;
            }
            if (XRequest.GetString("act") == "u")
            {
                if (XRequest.GetInt("cerid", -1) == -1 || XRequest.GetInt("aid", -1) == -1)
                {
                    Alert("数据参数错误");
                    rst = false; 
                }
            }
            //if (XRequest.GetInt("QuestionSlt", -1) == -1)
            //{
            //    Alert("忘记密码提示问题");
            //    rst = false;
            //}
            //if (XRequest.GetString("AnswerTxt").Equals(""))
            //{
            //    Alert("提示问题答案");
            //    rst = false;
            //}

            return rst;
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}
